<?php

namespace app\middleware;

use app\model\AdminAccount;
use app\model\User;
use Closure;
use think\facade\Cache;
use think\Request;

class ApiMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        $author = $request->header('Authorization', '[]');
        $author = json_decode($author, true);
        if (!empty($author) && Cache::has('api_' . $author['token'])) {
            //获取用户信息
            $uid = Cache::get('api_' . $author['token'])['uid'];
            $user = app(User::class)->where('id', $uid)->where('state', 1)->where('is_del', 1)->find();
            if (!empty($user)) $request->user = $user;
        }
        return $next($request);
    }
}
